package sz.szsmk.citizencard.mvp.presenter;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.QueryBalanceReq;
import sz.szsmk.citizencard.model.network.bean.request.QueryBalanceRes;
import sz.szsmk.citizencard.model.network.bean.request.QueryUserBindCardReq;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserBindCardRes;
import sz.szsmk.citizencard.mvp.contract.IQueryBalanceContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/13 19:41
 * description:
 */
public class QueryBalancePresenter extends IBasePresenter
        implements IQueryBalanceContact.IQueryBalancePresenter<IQueryBalanceContact.IQueryBalanceView> {
    private NetworkDateSources mNetworkDateSource;
    public static final String LOGIN_TEST = "{\"telephone\":\"@1\",\"agent\":\"@2\"}";
    GsdxNetApi gsdxNetApi;

    @Inject
    public QueryBalancePresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void querybalance(final String etCard) {
        QueryBalanceReq balanceReq = new QueryBalanceReq();
//        balanceReq.setCard_no("2150130204923640");
        balanceReq.setCard_no(etCard);
        balanceReq.setRequest_type("2");
        mNetworkDateSource.querybalance(balanceReq, new SubscribeResult<QueryBalanceRes>() {
            @Override
            protected void onOk(QueryBalanceRes response) {
                if (response == null) {
                    return;
                }
                ((IQueryBalanceContact.IQueryBalanceView) mView).querybalanceSucc(response,etCard);
            }

            @Override
            protected void onFailure(QueryBalanceRes response) {
                super.onFailure(response);
                ((IQueryBalanceContact.IQueryBalanceView) mView).querybalanceError(response);

            }

            @Override
            protected void onNext(QueryBalanceRes response) {
                super.onNext(response);
                ((IQueryBalanceContact.IQueryBalanceView) mView).querybalanceError(response);
            }

        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
//                ((IQueryBalanceContact.IQueryBalanceView) mView).querybalanceError(throwable.getMessage());

            }
        });

    }

    @Override
    public void queryBindCard() {
        QueryUserBindCardReq accessTokenRequest = new QueryUserBindCardReq();

        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        accessTokenRequest.setAccess_token(access_token);
        accessTokenRequest.setUser_type(0);
        mNetworkDateSource.queryBindCard(accessTokenRequest, new SubscribeResult<QueryUserBindCardRes>() {
            @Override
            protected void onOk(QueryUserBindCardRes response) {
                if (response.getUserCardEx()!=null){
                    ((IQueryBalanceContact.IQueryBalanceView) mView).queryBindCardSucc(response.getUserCardEx());
                }
            }

            @Override
            protected void onFailure(QueryUserBindCardRes response) {
                super.onFailure(response);
//                PubUtils.popTipOrWarn(DaggerApplication.getIns(),response.getMessage());
//                ((IQueryBalanceContact.IQueryBalanceView) mView).queryBindCardFail(response.getMessage());
            }

            @Override
            protected void onNext(QueryUserBindCardRes response) {
                super.onNext(response);
                PubUtils.popTipOrWarn(DaggerApplication.getIns(),response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                PubUtils.popTipOrWarn(DaggerApplication.getIns(),throwable.getMessage());

            }
        });

    }

}
